//================================================
//===        UART_communication                ===
//================================================
/*                  下行数据协议						
*///==============================================
int skipOut=0;
void serialOut_GUI() {  
  
  if(skipOut++>=updateRate) {                                                        
    skipOut = 0;
    //Filtered and unfiltered speed
    Serial.print(speeds, DEC);  Serial.print(",");
    Serial.print(speeds_filter, DEC);   Serial.print(",");

    //Raw sensor data
    Serial.print(Angle[0], DEC);   Serial.print(",");
    Serial.print(w[0], DEC);   Serial.print(",");
    Serial.print(a[0], DEC);   Serial.print(",");
   
    Serial.print(positions, DEC);      Serial.print(",");
    Serial.print(Item_limited, DEC);      Serial.print(",");
    Serial.print(-Item_limited, DEC);      Serial.print(",");
    Serial.print(Pwm_out, DEC);      Serial.print(",");
    Serial.print(Value_filter, DEC);      Serial.print(",");
    Serial.print(Item_limited, DEC);   Serial.print(",");
    
    //PID Parameters
    Serial.print(Kap, DEC);   Serial.print(",");
    Serial.print(Kad, DEC);   Serial.print(",");
    Serial.print(Ksp, DEC);   Serial.print(",");
    Serial.print(Ksi, DEC);   Serial.print(",");
    
    //loop
    Serial.print(STD_LOOP_TIME, DEC);        Serial.print(",");
    Serial.print(lastLoopUsefulTime, DEC);   Serial.print(",");
    Serial.print(lastLoopTime, DEC);         Serial.print(",");
    Serial.print(updateRate, DEC);           Serial.print(",");
    
    Serial.print(motorOffsetL, DEC);         Serial.print(",");
    Serial.print(motorOffsetR, DEC);         Serial.print("\n");
  }
}

union u_tag {
  byte b[4];
  float fval;
} u;

int skipIn=0;

void serialIn_GUI(){
  
  if(skipIn++>=updateRate) {                                                        
    skipIn = 0;
    byte id;
    
    if(Serial.available() > 0)
    {               
      char param = Serial.read(); 
      delay(10);
      byte inByte[Serial.available()];
      if(Serial.read() == SPLIT){
        if(Serial.available() >= 4){
          u.b[3] = Serial.read(); 
          u.b[2] = Serial.read(); 
          u.b[1] = Serial.read(); 
          u.b[0] = Serial.read(); 
          Serial.flush();
          
          switch (param) {
            case 'p':
              Kap = int(u.fval);
              break;
            case 'd':
              Kad = int(u.fval);
              break;
            case 'P':
              Ksp = int(u.fval);
              break;
            case 'I':
              Ksi = u.fval;
              break;
            case 's':
              Item_limited = int(u.fval);
              break;
            case 'u':
              updateRate = int(u.fval);
              break;
            case 'l':
              motorOffsetL = u.fval;
              break;
            case 'r':
              motorOffsetR = u.fval;
              break;
          }
        }
      }
    }
  }
}
